﻿using System;
using System.Data.SqlClient;
using System.Collections.Generic;

namespace DataCoreTechnologies.SqlServer
{
   /// <summary>
   /// Contains information about a view belonging to a schema.
   /// </summary>
   public sealed class View: DatabaseObject
   {
      #region Public
      //Properties
      /// <summary>
      /// Returns the T-Sql statement used to create this view.
      /// </summary>
      public string DDL { get; internal set; }
      /// <summary>
      /// A collection of columns belonging to this view.
      /// </summary>
      public IEnumerable<Column> Columns
      {
         get
         {
             for (int index = 0; index < _columns.Count; index++)
             {
                 yield return _columns[index];
             }
         }
      }
      /// <summary>
      /// A collection of triggers belonging to this view.
      /// </summary>
      public IEnumerable<Trigger> Triggers
      {
         get
         {
             for (int index = 0; index < _triggers.Count; index++)
             {
                 yield return _triggers[index];
             }
         }
      }
      /// <summary>
      /// A collection of indexes belonging to this view.
      /// </summary>
      public IEnumerable<Index> Indexes
      {
         get
         {
             for (int index = 0; index < _indexes.Count; index++)
             {
                 yield return _indexes[index];
             }
         }
      }
      /// <summary>
      /// A collection of extended properties belonging to this view.
      /// </summary>
      public IEnumerable<ExtendedProperty> ExtendedProperties
      {
         get
         {
             for (int index = 0; index < _extendedProperties.Count; index++)
             {
                 yield return _extendedProperties[index];
             }
         }
      }
      //Methods
      /// <summary>
      /// Returns a list of T-Sql tokens comprimising the create statement for this view.
      /// </summary>
      /// <returns>
      /// The list of T-Sql tokens comprimising the create statement for this view.
      /// </returns>
      public List<string> GetDdlTokens() { return TSql.GetDDLTokens(DDL); }
      #endregion Public
      #region Internal
      //Constructor
      /// <summary>
      /// Creates a view and associates it to the schema specified by the parameter.
      /// </summary>
      /// <param name="container">The schema this view belongs to.</param>
      internal View(Schema container)
      {
         base.Schema = container;
         base.InstanceId = System.Guid.NewGuid();
      }
      //Methods
      /// <summary>
      /// Associates a column to this view.
      /// </summary>
      /// <param name="column">The column that belongs to this view.</param>
      internal void AddColumn(Column column) { _columns.Add(column); }
      /// <summary>
      /// Associates a trigger to this view.
      /// </summary>
      /// <param name="trigger">The trigger that belongs to this view.</param>
      internal void AddTrigger(Trigger trigger) { _triggers.Add(trigger); }
      /// <summary>
      /// Associates a index to this view.
      /// </summary>
      /// <param name="index">The index that belongs to this view.</param>
      internal void AddIndex(Index index) { _indexes.Add(index); }
      /// <summary>
      /// Associates a extended property to this view.
      /// </summary>
      /// <param name="extendedProperty">The extended property that belongs to this view.</param>
      internal void AddExtendedProperty(ExtendedProperty extendedProperty) { _extendedProperties.Add(extendedProperty); }
      #endregion Internal
      #region Private
      //Properties
      private List<Column> _columns = new List<Column>();
      private List<Trigger> _triggers = new List<Trigger>();
      private List<ExtendedProperty> _extendedProperties = new List<ExtendedProperty>();
      private List<Index> _indexes = new List<Index>();
      #endregion
   }
}
